package com.masasdani.paypal.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import com.paypal.api.payments.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.masasdani.paypal.config.PaypalPaymentIntent;
import com.masasdani.paypal.config.PaypalPaymentMethod;
import com.paypal.base.rest.APIContext;

import com.paypal.base.rest.PayPalRESTException;

@Service
public class PaypalService {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private APIContext apiContext;
	
	public Payment createPayment(
			Double total, 
			String currency, 
			PaypalPaymentMethod method, 
			PaypalPaymentIntent intent, 
			String description, 
			String cancelUrl, 
			String successUrl) throws PayPalRESTException{
		Amount amount = new Amount();
		amount.setCurrency(currency);
		total = new BigDecimal(total).setScale(2, RoundingMode.HALF_UP).doubleValue();
		amount.setTotal(String.format("%.2f", total));

		Transaction transaction = new Transaction();
		transaction.setDescription(description);
		transaction.setAmount(amount);

		List<Transaction> transactions = new ArrayList<>();
		transactions.add(transaction);

		Payer payer = new Payer();
		payer.setPaymentMethod(method.toString());

		Payment payment = new Payment();
		payment.setIntent(intent.toString());
		payment.setPayer(payer);
		payment.setTransactions(transactions);
		RedirectUrls redirectUrls = new RedirectUrls();
		redirectUrls.setCancelUrl(cancelUrl);
		redirectUrls.setReturnUrl(successUrl);
		payment.setRedirectUrls(redirectUrls);

		return payment.create(apiContext);
	}
	
	public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{
		Payment payment = new Payment();
		payment.setId(paymentId);
		PaymentExecution paymentExecute = new PaymentExecution();
		paymentExecute.setPayerId(payerId);
		// 设置 PayPal-Request-Id 头部
		String requestId = generateRequestId(); // 生成唯一的 PayPal-Request-Id 值
		Map<String, String> headers = new HashMap<>();
		headers.put("PayPal-Request-Id", requestId);
		apiContext.setHTTPHeaders(headers);

		return payment.execute(apiContext, paymentExecute);
	}

	// 执行退款
	public Refund refund(String saleId,Double money, String currency,String reason) throws PayPalRESTException {
		String requestId = generateRequestId(); // 生成唯一的 PayPal-Request-Id 值
		// 设置 PayPal-Request-Id 头部
		Map<String, String> headers = new HashMap<>();
		headers.put("PayPal-Request-Id", requestId);
		apiContext.setHTTPHeaders(headers);

		Refund refund = new Refund();
		Amount amount = new Amount();
		amount.setCurrency(currency);
		amount.setTotal(String.valueOf(money));
		refund.setAmount(amount);
		refund.setReason(reason);
		Sale sale = new Sale();
		sale.setId(saleId);
		Refund refund1 = sale.refund(apiContext,refund);
		return refund1;
	}

	// 生成唯一的 PayPal-Request-Id 值
	private String generateRequestId() {
		return UUID.randomUUID().toString();
	}

}
